package com.ls.fw.hadoop;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileContext;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.UnresolvedLinkException;
import org.apache.hadoop.hdfs.DFSClient;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.LocatedBlock;
import org.apache.hadoop.hdfs.web.WebHdfsFileSystem;
import org.apache.hadoop.security.AccessControlException;

/**
 * hdfs服务
 * @author Administrator
 *
 */
public interface HdfsDao {

	/**
	 * 配置信息
	 * @return
	 */
	public Configuration getConfiguration();
	
	
	public void setConf(Configuration conf) throws Exception;
	
	/**
	 * 销毁
	 */
	public void destory();
	
	public Path[] getPaths(String... path);
	
	public Path getPath(String path);
	
	public Path getPath(URI uri);
	
	public Path getPath(String parent, String child);
	
	public DistributedFileSystem getDistributedFileSystem();
	
	public FileSystem getFileSystem();
	
	public FileContext getFileContext();
	
	//public HftpFileSystem getHftpFileSystem(); 
	
	public DFSClient getDFSClient();
	
	
	public WebHdfsFileSystem getWebHdfsFileSystem(); 
	
	/**
	 * 文件是否存在
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public  boolean isExist(String path) throws Exception;
	
	
	/**
	 * 获取文件状态
	 * @param path
	 * @return
	 * @throws IOException 
	 */
	public FileStatus[] list(String... path) throws IOException;
	
	
	/**
	 * 获取文件状态
	 * @param srcPath
	 * @return
	 * @throws IOException
	 */
	public  FileStatus getFileStatus(String srcPath) throws IOException;
	
	/**
	 * 获取读取流
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public  FSDataInputStream getInputStream(String path) throws Exception;


	/**
	 * 追加
	 * @param srcPath
	 * @param destPath
	 * @throws Exception
	 */
	void append(String srcPath, String destPath) throws Exception;


	/**
	 * 重新命名文件
	 * @param srcPath
	 * @param destPath
	 * @return
	 * @throws Exception
	 */
	boolean rename(String srcPath, String destPath) throws Exception;


	/**
	 * 创建目录
	 * @param path
	 * @return
	 * @throws Exception
	 */
	boolean mkdirs(String path) throws Exception;


	/**
	 * 创建新文件
	 * @param path
	 * @return
	 * @throws Exception
	 */
	boolean createNewFile(String path) throws Exception;


	/**
	 * 删除
	 * @param path
	 * @return
	 * @throws IOException
	 */
	boolean delete(String path) throws IOException;


	/**
	 * 删除文件夹
	 * @param path
	 * @param recursive
	 * @return
	 * @throws IOException
	 */
	boolean deleteDir(String path, boolean recursive) throws IOException;


	/**
	 * 删除文件
	 * @param path
	 * @return
	 * @throws IOException
	 */
	boolean deleteFile(String path) throws IOException;


	/**
	 * 获取输入流
	 * @param path
	 * @return
	 * @throws Exception
	 */
	FSDataOutputStream getOutputStream(String path) throws Exception;


	/**
	 * 下载
	 * @param srcPath
	 * @param destDir
	 * @throws Exception
	 */
	void down(String srcPath, String destDir) throws Exception;


	/**
	 * 上传
	 * @param srcPath
	 * @param destDir
	 * @throws Exception
	 */
	void upload(String srcPath, String destDirOrdestPath) throws Exception;


	/**
	 * 上传
	 * @param inStream
	 * @param destPath 文件路径
	 * @throws Exception
	 */
	void upload(InputStream inStream, String destPath) throws Exception;


	/**
	 * 下载
	 * @param request
	 * @param response
	 * @param path
	 * @param name
	 * @throws IOException
	 */
	void down(HttpServletRequest request, HttpServletResponse response,
			String path, String name) throws IOException;


	
	/**
	 * 获取文件块地址信息
	 * @param fileStatus
	 * @return
	 * @throws IOException
	 */
	BlockLocation[] getBlockLocation(FileStatus fileStatus) throws IOException;


	/**
	 * 获取文件块地址信息
	 * @param path
	 * @return
	 * @throws IOException
	 */
	BlockLocation[] getBlockLocation(String path) throws IOException;


	WebHdfsFileSystem openWebHdfsFileSystem() throws IOException,
			URISyntaxException;


	//HftpFileSystem openHftpFileSystem() throws IOException, URISyntaxException;


	List<LocatedBlock> getLocatedBlock(String path)
			throws AccessControlException, FileNotFoundException,
			UnresolvedLinkException, IOException;
}
